var ColoredButton = (function () {
    //继承ui.Widget
    util.extend(ColoredButton, ui.Widget);

    function ColoredButton() {
        //调用父类构造函数
        ui.Widget.call(this);
        //自定义属性color，定义按钮颜色
        this.defineAttr("color", (view, name, defaultGetter) => {
            return this._color;
        }, (view, name, value, defaultSetter) => {
            this._color = value;
            view.attr("backgroundTint", value);
        });
        //自定义属性onClick，定义被点击时执行的代码
        this.defineAttr("onClick", (view, name, defaultGetter) => {
            return this._onClick;
        }, (view, name, value, defaultSetter) => {
            this._onClick = value;
        });
    }
    ColoredButton.prototype.render = function () {
        return (
            <button textSize="16sp" style="Widget.AppCompat.Button.Colored" w="*" h="55" id="login" />
        );
    }
    ColoredButton.prototype.onViewCreated = function (view) {
        view.on("click", () => {
            if (this._onClick) {
                eval(this._onClick);
            }
        });
    }
    ui.registerWidget("colored-button", ColoredButton);
    return ColoredButton;
})();
var s = false;
var popWindow = ""
var storage = storages.create("data");
var mainActivityClassName = ""
function showWindow() {
    popWindow = floaty.window(
        <relative w="*" h="100" id="relative">
            <frame id="parent" w="*" h="100" />
            <frame id="child" width="250" height="100"
                layout_alignParentRight="true"
                layout_alignParentTop="true"
                alpha="0.7">
                <horizontal w="*" h="*">
                    <frame w="200" h="100" bg="#ffffff">
                        <text id="text" singleLine="false" color="#000000" size="12" padding="10" w="*" h="*" /><text />
                    </frame>
                    <button id="btn" text="关闭" w="50" h="100" />
                </horizontal>
            </frame>
        </relative>
    );
    popWindow.setSize(-1, -2)
    popWindow.exitOnClose();

    popWindow.relative.click(() => {
        popWindow.setAdjustEnabled(!popWindow.isAdjustEnabled());
    });
    popWindow.btn.click(() => {
        clearActive()
    })
}
ui.layout(
    <vertical>
        <text text="大抖音" textSize="20sp" textStyle="bold" textColor="black" gravity="center" margin="100" />
        <frame padding="30 0 30 0" margin="0 20 0 20">
            <colored-button text="启动脚本" onClick="startTask()" color="black" />
        </frame>
        <frame padding="30 0 30 0" marginBottom="20">
            <colored-button text="停止脚本" onClick="clear()" color="#cccccc" />
        </frame>
    </vertical>
)
function clear() {
    toast('脚本已停止')
    if(popWindow){
        popWindow.close()
    }
    setTimeout(() => {
        // threads.start(()=>{
        //     threads.exitAll()
        // })
        exit()
    }, 500);
}

function startTask() {
    device.keepScreenOn()
    mainActivityClassName = currentActivity()
    try {
        auto() // 无障碍处理
        let ram = device.getAvailMem() / 1000 / 1000 / 1000
        if (ram < 0.5) {
            confirm('当前内存严重不足，请释放后台后再运行')
            return
        }
        confirm('确认抖音已登录账户并给予权限，避免功能使用出错').then((val) => {
            if (val) {
                threads.start(function () {
                    if(!popWindow){
                       showWindow() 
                    }
                    dy.init()
                })
            }
        })
    } catch (error) {
        confirm('如果无法解决问问，可能是安卓的BUG，您可能需要重启手机或重启设置中无障碍服务')
    }
}
// 获取版本信息
function getSystemVersion() {
    // return android.os.Build.VERSION.RELEASE;
    // api.getVersion({}, (res) => {
    //     let version = res.json().data.activity.id;
    //     if(!ui.version){
    //         return
    //     }
    //     ui.run(() => {
    //         ui.version.setText('版本V' + version)
    //     })
    // })
    ui.version.setText('版本V 1.0.0')
}
function clearActive() {
    //test
    dy.clearTask()
    threads.start(function () {
        app.startActivity({
            packageName: 'com.dadouyin.app',
            className: mainActivityClassName,
            flags: [Intent.FLAG_ACTIVITY_NEW_TASK] // 可选标志，确保新任务  
        });
    })
    toast('脚本已停止运行')
}
// getSystemVersion()
//登录事件

module.exports = {
    setTopText(desc) {
        ui.run(function () {
            let oldDesc = (popWindow.text.getText() + "\n").split("\n");
            oldDesc = oldDesc.filter((item) => item.trim() !== '')
            let oldDescFormat = oldDesc.slice(-1).map((item) => item + '\n').join('')
            popWindow.text.setText(oldDescFormat + desc);
        });
    }
}